#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <string>
#include <memory>
#include <cstdlib>

// 默认尝试重连次数
#define Retry_Count 5   

using namespace std; 

void Usage(const std::string& proc)
{
    std::cout << "Usage : \n\t" << proc << " server_ip server_port\n" << std::endl;
}

bool VisitServer(std::string &serverip, uint16_t serverport, int *pcnt)
{
    bool ret = true;    // 返回值
    ssize_t wn;          // 接收write返回值
    ssize_t rn;          // 接收read返回值
    std::string inbuffer;   // 输入缓冲区
    char buffer[1024];         // 网络缓冲区
    // 1. 创建socket
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        std::cerr << "socket error" << std::endl;
        return false;
    }

    // 2. connect
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    inet_pton(AF_INET, serverip.c_str(), &server.sin_addr); // 更安全
    
    int n = connect(sockfd, (struct sockaddr*)&server, sizeof(server)); // 自动进行bind
    if(n < 0)
    {
        std::cerr << "connect error" << std::endl;
        ret = false;
        goto END;
    }

    *pcnt = 1;     //重置cnt，注意重置的位置要在connect之后

    rn = read(sockfd, buffer, sizeof(buffer) - 1);
    if(rn > 0)
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
        buffer[rn] = 0;
        std::cout << buffer << std::endl;
    }
    else
    {
        ret = false;
        goto END;
    }

    std::cout << "Please Enter# ";
    getline(std::cin, inbuffer);
    wn = write(sockfd, inbuffer.c_str(), inbuffer.size());
    if(wn > 0)
    {
        rn = read(sockfd, buffer, sizeof(buffer) - 1);
        buffer[rn] = 0;
        std::cout << buffer << std::endl;
        if(!strcmp(buffer, "no find"))     // 没有找到对应服务就直接返回
            goto END;

        std::cout << "[service on] Please Enter# ";
        getline(std::cin, inbuffer);
        write(sockfd, inbuffer.c_str(), inbuffer.size());
        rn = read(sockfd, buffer, sizeof(buffer) - 1);
        if(rn > 0)
        {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
            buffer[rn] = 0;
            std::cout << buffer << std::endl;
        }
        else
        {
            ret = false;
            goto END;
        }
    }
    else if(wn == 0)
    {
        // donothing
    }
    else 
    {
        ret = false;
        goto END;
    }
    

END:
    close(sockfd);
    return ret;
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        return 1;
    }

    std::string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);

    // 连接服务器失败时，进行Retry_Count次重连
    int cnt = 1;
    while(cnt <= Retry_Count)
    {
        bool result = VisitServer(serverip, serverport, &cnt); // 将cnt传入，方便重置
        if(result)
        {
            break;
        }
        else
        {
            sleep(1);
            std::cout << "server offline, retrying..., count : " << cnt++ << std::endl;
        }
    }

    if(cnt >= Retry_Count)
    {
        std::cout << "server offline, client quit..." << std::endl;
    }

    return 0;
}